using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL.Fabric
{
    public class CombineBlock : Block
    {
        int iInputs=0;
        int iOutputWidth = 0;

        public CombineBlock(string psName, params int[] piWidths)
            : base(psName,"COMBINE")
        {
            foreach(int iWidth in piWidths)
            {
                iInputs++;
                string sName = "IN" + iInputs.ToString();
                CreateInput(sName,iWidth);
                iOutputWidth += iWidth;
            }
            CreateOutput("OUT", iOutputWidth);
        }

        internal override void GenerateStructure()
        {
            NodeVector nvOut = ParentComponent.CreateNodeVector(Name + "_DATA_O", iOutputWidth);
            Output oOutput = Outputs["OUT"];
            oOutput.LinkNodeVector(nvOut);
            int iLeft = 0;
            for (int iInput = 1; iInput <= iInputs; iInput++)
            {
                string sName = "IN" + iInput.ToString();
                Input oInput = Inputs[sName];
                NodeVector nvIn = ParentComponent.CreateNodeVector(Name + "_DATA" + iInput.ToString() + "_I", oInput.Width);
                oInput.LinkNodeVector(nvIn);
                oOutput.NodeVector[iLeft, iLeft + oInput.Width - 1].Join(nvIn);
                iLeft += oInput.Width;
            }
        }

    }
}
